//
//  ContactDao.m
//  Contacts
//
//  Created by 武 娇 on 14-7-27.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "ContactDao.h"
#import "Contact.h"
#import "Department.h"
@implementation ContactDao
-(BOOL)insertContact:(Contact *)contact{
    BOOL success = YES;
	[db executeUpdate:[self SQL:@"INSERT INTO %@ (id,name,login_name,phone,mobilephone,department_id,department_name,office_num,is_activity,position,token,password,head_name,name_pinyin,head_url,mark) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)" inTable:Contact_TABLE],
       @(contact.userID),
     contact.name,
     contact.loginName,
     contact.phone,
     contact.mobilephone,
     @(contact.department.ID),
     contact.department.name,
     contact.officeNo,
     [NSNumber numberWithBool:contact.isActivity],
     contact.position,
     contact.token,
     contact.password,
     contact.headName,
     contact.namePinyin,
     contact.headUrl,
     contact.mark
     ];
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
		success = NO;
    }
	return success;
}

-(BOOL)updateContact:(Contact *)contact{
    BOOL success = YES;
    [db executeUpdate:[self SQL:@"UPDATE %@ SET phone =?, mobilephone =?,department_id =?,department_name =?,office_num =?,is_activity =?,position =?,password =?,head_name =?, head_url =?,mark = ? WHERE id = ?" inTable:Contact_TABLE],
     contact.phone,
     contact.mobilephone,
     @(contact.department.ID),
     contact.department.name,
     contact.officeNo,
     [NSNumber numberWithBool:contact.isActivity],
     contact.position,
     contact.password,
     contact.headName,
     contact.headUrl,
     contact.mark,
     @(contact.userID)
     ];
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
		success = NO;
    }
	return success;
}

-(BOOL)deleteContact:(Contact *)contact{
    BOOL success = NO;
    [db executeUpdate:[self SQL:@"delete from %@ where id =?" inTable:Contact_TABLE],
      @(contact.userID)
     ];
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
		success = NO;
    }
    return success;
}

-(BOOL)insertContacts:(NSArray *)contacts{
    BOOL success = YES;
    for (Contact *contact in contacts) {
        [self insertContact:contact];
        if ([db hadError]) {
            NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
            success = NO;
            break;
        }
    }
	return success;
}

-(BOOL)updateContacts:(NSArray *)contacts{
    BOOL success = YES;
    for (Contact *contact in contacts) {
        [self updateContact:contact];
        if ([db hadError]) {
            NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
            success = NO;
            break;
        }
    }
	return success;
}

-(NSArray *)searchContactsWithKey:(NSString *)key{
    NSMutableArray *contacts = nil;
    NSString *pro = [NSString stringWithFormat:@"select * from %@ where ",Contact_TABLE];
    NSString *likeBef = @"\%";
    NSString *likeAft = @"\%";
    NSString *phone = [NSString stringWithFormat:@"phone like \'%@%@%@\'",likeBef,key,likeAft];
    NSString *mobilephone = [NSString stringWithFormat:@"mobilephone like \'%@%@%@\'",likeBef,key,likeAft];
    NSString *name = [NSString stringWithFormat:@"name like \'%@%@%@\'",likeBef,key,likeAft];
    NSString *departmentname = [NSString stringWithFormat:@"department_name like \'%@%@%@\'",likeBef,key,likeAft];
    NSString *namePinyin = [NSString stringWithFormat:@"name_pinyin like \'%@%@%@\'",likeBef,key,likeAft];
    NSString *sql = [pro stringByAppendingFormat:@"%@ or %@ or %@ or %@ or %@",name,phone,mobilephone,departmentname,namePinyin];
 	FMResultSet *rs =[db executeQuery:sql];
    
	while ([rs next]) {
        if (!contacts) {
            contacts = [NSMutableArray arrayWithCapacity:0];
        }
        Contact *contact = [self analysisWithRS:rs];
        [contacts addObject:contact];
    }
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
	}
    return contacts;
}

-(Contact *)getContactWithUserID:(NSInteger)userID{
    Contact *contact = nil;
    FMResultSet *rs =[db executeQuery:[self SQL:@"select * from %@ WHERE id = ?" inTable:Contact_TABLE],@(userID)];
    while ([rs next]) {
        contact = [self analysisWithRS:rs];
    }
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
	}
    return contact;
}
-(Contact *)getContactWithContactID:(NSInteger)contactID{
    Contact *contact = nil;
    FMResultSet *rs =[db executeQuery:[self SQL:@"select * from %@ WHERE id = ?" inTable:Contact_TABLE],@(contactID)];
    while ([rs next]) {
        contact = [self analysisWithRS:rs];
    }
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
	}
    return contact;
}
-(NSArray *)getContactsWithDepartmentID:(NSInteger)departmentID{
    FMResultSet *rs =[db executeQuery:[self SQL:@"select * from %@ WHERE department_id = ?" inTable:Contact_TABLE],@(departmentID)];
    NSMutableArray *contacts = nil;
    while ([rs next]) {
        if (!contacts) {
            contacts = [NSMutableArray arrayWithCapacity:10];
        }
        Contact *contact =[self analysisWithRS:rs];
        [contacts addObject:contact];
    }
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
	}
    return contacts;
}
-(BOOL)clearContacts{
    BOOL success = YES;
	[db executeUpdate:[self SQL:@"delete from %@" inTable:Contact_TABLE]];
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
        success = NO;
	}
    return success;
}
-(Contact *)analysisWithRS:(FMResultSet *)rs{
    Contact *contact = [[Contact alloc]init];
    contact.userID= [rs intForColumn:@"id"];
    contact.name = [rs stringForColumn:@"name"];
    contact.phone = [rs stringForColumn:@"phone"];
    contact.mobilephone =[rs stringForColumn:@"mobilephone"];
    contact.officeNo = [rs stringForColumn:@"office_num"];
    Department *department = [[Department alloc]init];
    department.ID = [rs intForColumn:@"department_id"];
    department.name = [rs stringForColumn:@"department_name"];
    contact.department = department;
    contact.isActivity = [rs boolForColumn:@"is_activity"];
    contact.token =[rs stringForColumn:@"token"];
    contact.password =[rs stringForColumn:@"password"];
    contact.loginName = [rs stringForColumn:@"login_name"];
    contact.headName = [rs stringForColumn:@"head_name"];
    contact.headUrl = [rs stringForColumn:@"head_url"];
    contact.position = [rs stringForColumn:@"position"];
    contact.mark = [rs stringForColumn:@"mark"];
    return contact;
}
@end
